// 使用简单别名映射，避免 Node 内置模块类型依赖

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import viteCompression from 'vite-plugin-compression'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    // Element Plus 自动导入
    AutoImport({
      resolvers: [ElementPlusResolver()],
      imports: ['vue', 'vue-router', 'pinia'],
      dts: 'src/auto-imports.d.ts',
    }),
    Components({
      resolvers: [ElementPlusResolver()],
      dts: 'src/components.d.ts',
    }),
    // Gzip压缩
    viteCompression({
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: '.gz',
    }),
  ],
  resolve: {
    alias: {
      '@': '/src',
    },
  },
  server: {
    port: 5173,
    host: '0.0.0.0',
    open: true,
    cors: true,
    proxy: {
      '/api': {
        target: 'http://localhost:8000',
        changeOrigin: true,
        secure: false,
        ws: true,
        // 按 Vite 类型使用 configure 挂载错误处理
        configure: (proxy) => {
          proxy.on('error', (err) => {
            console.warn('代理错误已处理:', (err as Error).message)
          })
        },
      },
    },
  },
  build: {
    target: 'es2015',
    outDir: 'dist',
    sourcemap: false,
    chunkSizeWarningLimit: 600, // 提高警告阈值到 600KB
    rollupOptions: {
      output: {
        // 优化的代码分割策略
        manualChunks: (id) => {
          // Node modules 分组策略
          if (id.includes('node_modules')) {
            // Element Plus 相关
            if (id.includes('element-plus')) {
              // 将 Element Plus 核心和组件分离
              if (id.includes('@element-plus/icons-vue')) {
                return 'element-icons'
              }
              return 'element-plus'
            }

            // ECharts 相关
            if (id.includes('echarts') || id.includes('zrender')) {
              return 'echarts'
            }

            // Vue 生态系统
            if (id.includes('vue-router')) {
              return 'vue-router'
            }
            if (id.includes('pinia')) {
              return 'pinia'
            }
            if (id.includes('vue-echarts')) {
              return 'vue-echarts'
            }

            // 日期处理库
            if (id.includes('dayjs') || id.includes('date-fns')) {
              return 'date-utils'
            }

            // Axios 和网络请求
            if (id.includes('axios')) {
              return 'axios'
            }

            // 其他第三方库统一放到 vendor
            return 'vendor'
          }
        },
        // 输出文件命名优化
        chunkFileNames: 'js/[name]-[hash].js',
        entryFileNames: 'js/[name]-[hash].js',
        assetFileNames: (assetInfo) => {
          if (assetInfo.name?.endsWith('.css')) {
            return 'css/[name]-[hash][extname]'
          }
          if (/\.(png|jpe?g|gif|svg|ico|webp)$/i.test(assetInfo.name || '')) {
            return 'img/[name]-[hash][extname]'
          }
          if (/\.(woff2?|eot|ttf|otf)$/i.test(assetInfo.name || '')) {
            return 'fonts/[name]-[hash][extname]'
          }
          return 'assets/[name]-[hash][extname]'
        },
      },
    },
  },
})
